﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Code;
using System.Linq.Expressions;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin
{
    public partial class ActivityMediaList : BasePage
    {
        /// <summary>
        /// 查看新闻的系统字典类型
        /// </summary>
        public string SystemType
        {
            get
            {
				//if (ViewState["SystemType"] == null)
				//    return "00020004";//要闻导读，默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
            }
            private set
            {
				ViewState["SystemType"] = this.SystemCode;
            }
        }

        /// <summary>
        /// 是否是扩展编辑
        /// </summary>
        public bool ExEdit
        {
            get
            {
                if (ViewState["EditType"] == null)
                {
                    return false;
                }
                else
                {
                    return Convert.ToBoolean(ViewState["EditType"]);
                }
            }
            private set
            {
                ViewState["EditType"] = value;
            }
        }

        /// <summary>
        /// 是否发布到英文站点
        /// </summary>
        public bool IsEN
        {
            get
            {
                if (ViewState["IsEN"] == null)
                {
                    return false;
                }
                else
                {
                    return Convert.ToBoolean(ViewState["IsEN"]);
                }
            }
            private set
            {
                ViewState["IsEN"] = value;
            }
        }
        public int NewsID
        {
            get
            {
                if (ViewState["NewsID"] == null)
                {
                    return -1;
                }
                else
                {
                    return Convert.ToInt32(ViewState["NewsID"]);
                }
            }
            private set
            {
                ViewState["NewsID"] = value;
            }
        }

        public int NewsID_a
        {
            get
            {
                if (ViewState["NewsID_a"] == null)
                {
                    return -1;
                }
                else
                {
                    return Convert.ToInt32(ViewState["NewsID_a"]);
                }
            }
            private set
            {
                ViewState["NewsID_a"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, SystemCode))
                {
                    AdminBasePage.RedirectWithMessage(AdminDefaultPage, "您没有访问当前页面的权限,请与系统管理员联系!");
                    return;
                }
                if (!string.IsNullOrWhiteSpace(Request["st"]))
                {
                    this.SystemType = Request["st"].Trim();
                }

                if (!string.IsNullOrWhiteSpace(Request["ex"]))
                {
                    this.ExEdit = Request["ex"].Trim() == "1";
                }
                else
                {
                    this.ExEdit = false;
                }

                if (!string.IsNullOrWhiteSpace(Request["en"]))
                {
                    this.IsEN = Request["en"].Trim() == "1";
                }
                else
                {
                    this.IsEN = false;
                }
                int newsID = 0;
                if (!string.IsNullOrWhiteSpace(Request["id"]) && int.TryParse(Request["id"], out newsID))
                {
                    this.NewsID = newsID;
                }
                int newsID_a = 0;
                if (!string.IsNullOrWhiteSpace(Request["aid"]) && int.TryParse(Request["aid"], out newsID_a))
                {
                    this.NewsID_a = newsID_a;
                }
                BindData(1);
            }
        }

        private void BindData(int pageIndex)
        {
            Expression<Func<TB_MediaReports, bool>> f = (p => p.ActivityID == this.NewsID);
            Expression<Func<TB_MediaReports, DateTime>> o = (p => p.ReportDate);
            Expression<Func<TB_MediaReports, string>> then = (p => p.MediaName);
            int records = 0;
			var newsList = DAL.DALHelper.Instance.GetList<TB_MediaReports, DateTime, string>(f, o, false, then, true, this.Pager1.PageSize, pageIndex, out records);
            this.rpt_News.DataSource = newsList;
            this.rpt_News.DataBind();
            this.Pager1.RecordCount = records;
            this.Pager1.InitControl();
        }

        public void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
        {
            BindData(nextPage);
        }

        protected void btn_Delete_Click(object sender, EventArgs e)
        {
            int newsID = 0;
            if (!string.IsNullOrWhiteSpace(this.hid_ID.Value) && int.TryParse(this.hid_ID.Value, out newsID))
            {
                DAL.DALHelper.Instance.DeleteWithTransaction<TB_MediaReports>("ID", newsID);
                BindData(1);
            }
            this.hid_ID.Value = string.Empty;
        }

		protected void btn_NewsRefresh_Click(object sender, EventArgs e)
		{
			BindData(this.Pager1.CurrentPageIndex);
		}
    }
}